package com.ev.apis.controller.scm;

import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.annotation.RecoverRemoveData;
import com.ev.framework.config.Constant;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.framework.utils.R;
import com.ev.scm.domain.ProjectDO;
import com.ev.scm.service.ProjectService;
import com.ev.scm.vo.ProjectPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 文件名称： com.ev.scm.ProjectApiController.java</br>
 * 初始作者： ev-monitor</br>
 * 创建日期： 2020-07-02 16:32:19</br>
 * 功能说明： 项目 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 * Copyright (c) 2020-2021 .All rights reserved.<br/>
 */

@Api(value = "/", tags = "项目API")
@RestController
public class ProjectApiController {

    /**
     * 项目 服务类
     */
    @Autowired
    private ProjectService projectService;
    @Autowired
    private MessageSourceHandler messageSourceHandler;


    /**
     * 方法描述: [根据条件分页查询项目.]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-07-02 16:32:19<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param pageParam 分页参数
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/project/pageList", method = RequestMethod.POST, apiTitle = "根据条件分页查询项目")
    @ApiOperation(value = "根据条件分页查询项目", produces = MediaType.APPLICATION_JSON_VALUE)
    public R pageList(@Valid @RequestBody @ApiParam("项目分页列表参数对象") ProjectPageParam pageParam) {
        return R.ok(this.projectService.pageList(pageParam));
    }

    /**
     * 方法描述: [根据id获取项目.]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-07-02 16:32:19<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param id 主键
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/project/detail/{id}", method = RequestMethod.GET, apiTitle = "根据id获取项目")
    @ApiOperation("根据id获取项目")
    public R detail(@PathVariable("id") @ApiParam("id") Long id) {
        return R.ok(this.projectService.getById(id));
    }

    /**
     * 方法描述: [根据id审核项目.]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-07-02 16:32:19<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param id 主键
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/project/audit/{id}", method = RequestMethod.GET, apiTitle = "根据id审核项目")
    @ApiOperation("根据id审核项目")
    public R audit(@PathVariable("id") @ApiParam("id") Long id) {
        return this.projectService.audit(id);
    }


    /**
     * 方法描述: [根据id反审核项目.]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-07-02 16:32:19<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param id 主键
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/project/reverseAudit/{id}", method = RequestMethod.GET, apiTitle = "根据id反审核项目")
    @ApiOperation("根据id反审核项目")
    public R reverseAudit(@PathVariable("id") @ApiParam("id") Long id) {
        return this.projectService.reverseAudit(id);
    }


    /**
     * 方法描述: [批量删除]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-07-02 16:32:19<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param ids ID组
     * @return R 响应对象
     */
    @EvApiByToken(value = "/apis/project/batchRemove", method = RequestMethod.POST, apiTitle = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除", produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    public R batchRemove(@RequestBody @ApiParam("批量删除") List<Long> ids) {
        if (ids.size() > 0) {
            List<ProjectDO> projectDOList = projectService.getByIds(ids.toArray(new Long[0]));
            boolean b = projectDOList.stream().anyMatch(e -> Objects.equals(e.getAuditSign(), Constant.OK_AUDITED));
            if (b){
                return R.error(messageSourceHandler.getMessage("common.submit.delete.disabled",null));
            }
            projectDOList.forEach(e->e.setDelFlag(1));

            return this.projectService.batchUpdate(projectDOList) > 0 ? R.ok() : R.error();
        }
        return R.error();
    }

    /**
     * 方法描述: [保存项目.]</br>
     * 初始作者: ev-monitor<br/>
     * 创建日期: 2020-07-02 16:32:19<br/>
     * 开始版本: 1.0.0<br/>
     * =================================================<br/>
     * 修改记录：<br/>
     * 修改作者 日期 修改内容<br/>
     * ================================================<br/>
     *
     * @param saveParam 项目参数
     * @return R 响应对象
     */
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/project/save", method = RequestMethod.POST, apiTitle = "保存项目")
    @ApiOperation("保存项目")
    public R save(@Valid @RequestBody @ApiParam("保存项目对象") ProjectDO saveParam) {
        return this.projectService.saveAndVerify(saveParam);
    }

    @RecoverRemoveData(tableName = "scm_project",columnName = "del_flag",columnValue = "0")
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/project/recoverRemoveData",method = RequestMethod.POST,apiTitle = "恢复删除信息")
    @ApiOperation("恢复删除信息")
    public R recoverRemoveData(@ApiParam(value = "主键数组",required = true, example = "[1,2,3,4]") @RequestParam(value="ids",defaultValue = "") Long[] ids){
        // 切片统一处理
        return R.ok();
    }

}
